package com.harmay.center.monitor.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.util.Pool;

/**
 * @author Zuper
 * @version 1.0
 * @date 2022/6/16 11:20
 */
@Configuration
@EnableConfigurationProperties(RedisProperties.class)
public class RedisConfigFactory {
    @Autowired
    private RedisProperties redisProperties;

    @Bean
    @ConditionalOnClass(RedisProperties.class)
    public Pool<Jedis> redisBeanFactory() {
        JedisPoolConfig pool = new JedisPoolConfig();
        pool.setMaxIdle(5);
        pool.setMaxTotal(redisProperties.getMaxPoolSize());
        pool.setTestOnBorrow(true);
        pool.setTestOnReturn(false);
        pool.setTestWhileIdle(false);
        return new JedisPool(pool,redisProperties.getHost(),redisProperties.getPort(),redisProperties.getTimeout(),redisProperties.getPassword(),redisProperties.getDatabase());
    }
}
